home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_gettext.idb / usr / freeware / share / gettext / intl-dcgettext.c.z / intl-dcgettext.c
Encoding:
C/C++ Source or Header  |  1999-04-16  |  13.9 KB  |  525 lines

  1. /* dcgettext.c -- implemenatation of the dcgettext(3) function
  2.    Copyright (C) 1995 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  17.  
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21.  
  22. #include <sys/types.h>
  23.  
  24. #ifdef __GNUC__
  25. # define alloca __builtin_alloca
  26. #else
  27. # if defined HAVE_ALLOCA_H || defined _LIBC
  28. #  include <alloca.h>
  29. # else
  30. #  ifdef _AIX
  31.  #pragma alloca
  32. #  else
  33. #   ifndef alloca
  34. char *alloca ();
  35. #   endif
  36. #  endif
  37. # endif
  38. #endif
  39.  
  40. #include <errno.h>
  41. #ifndef errno
  42. extern int errno;
  43. #endif
  44.  
  45. #if defined STDC_HEADERS || defined _LIBC
  46. # include <stdlib.h>
  47. #else
  48. char *getenv ();
  49. # ifdef HAVE_MALLOC_H
  50. #  include <malloc.h>
  51. # else
  52. void free ();
  53. # endif
  54. #endif
  55.  
  56. #if defined HAVE_STRING_H || defined _LIBC
  57. # include <string.h>
  58. #else
  59. # include <strings.h>
  60. #endif
  61. #if !HAVE_STRCHR && !defined _LIBC
  62. # ifndef strchr
  63. #  define strchr index
  64. # endif
  65. #endif
  66.  
  67. #if defined HAVE_UNISTD_H || defined _LIBC
  68. # include <unistd.h>
  69. #endif
  70.  
  71. #include "gettext.h"
  72. #include "gettextP.h"
  73. #ifdef _LIBC
  74. # include <libintl.h>
  75. #else
  76. # include "libgettext.h"
  77. #endif
  78. #include "hash-string.h"
  79.  
  80. /* @@ end of prolog @@ */
  81.  
  82. #ifdef _LIBC
  83. /* Rename the non ANSI C functions.  This is required by the standard
  84.    because some ANSI C functions will require linking with this object
  85.    file and the name space must not be polluted.  */
  86. # define getcwd __getcwd
  87. # define stpcpy __stpcpy
  88. #endif
  89.  
  90. #if !defined HAVE_GETCWD && !defined _LIBC
  91. char *getwd ();
  92. # define getcwd(buf, max) getwd (buf)
  93. #else
  94. char *getcwd ();
  95. #endif
  96.  
  97. /* Amount to increase buffer size by in each try.  */
  98. #define PATH_INCR 32
  99.  
  100. /* The following is from pathmax.h.  */
  101. /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
  102.    PATH_MAX but might cause redefinition warnings when sys/param.h is
  103.    later included (as on MORE/BSD 4.3).  */
  104. #if defined(_POSIX_VERSION) || (defined(HAVE_LIMITS_H) && !defined(__GNUC__))
  105. # include <limits.h>
  106. #endif
  107.  
  108. #ifndef _POSIX_PATH_MAX
  109. # define _POSIX_PATH_MAX 255
  110. #endif
  111.  
  112. #if !defined(PATH_MAX) && defined(_PC_PATH_MAX)
  113. # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
  114. #endif
  115.  
  116. /* Don't include sys/param.h if it already has been.  */
  117. #if defined(HAVE_SYS_PARAM_H) && !defined(PATH_MAX) && !defined(MAXPATHLEN)
  118. # include <sys/param.h>
  119. #endif
  120.  
  121. #if !defined(PATH_MAX) && defined(MAXPATHLEN)
  122. # define PATH_MAX MAXPATHLEN
  123. #endif
  124.  
  125. #ifndef PATH_MAX
  126. # define PATH_MAX _POSIX_PATH_MAX
  127. #endif
  128.  
  129. /* XPG3 defines the result of `setlocale (category, NULL)' as:
  130.    ``Directs `setlocale()' to query `category' and return the current
  131.      setting of `local'.''
  132.    However it does not specify the exact format.  And even worse: POSIX
  133.    defines this not at all.  So we can use this feature only on selected
  134.    system (e.g. those using GNU C Library).  */
  135. #ifdef _LIBC
  136. # define HAVE_LOCALE_NULL
  137. #endif
  138.  
  139. /* Name of the default domain used for gettext(3) prior any call to
  140.    textdomain(3).  The default value for this is "messages".  */
  141. const char _nl_default_default_domain[] = "messages";
  142.  
  143. /* Value used as the default domain for gettext(3).  */
  144. const char *_nl_current_default_domain = _nl_default_default_domain;
  145.  
  146. /* Contains the default location of the message catalogs.  */
  147. const char _nl_default_dirname[] = GNULOCALEDIR;
  148.  
  149. /* List with bindings of specific domains created by bindtextdomain()
  150.    calls.  */
  151. struct binding *_nl_domain_bindings;
  152.  
  153. /* Prototypes for local functions.  */
  154. static char *find_msg __P ((struct loaded_domain *domain, const char *msgid));
  155. static const char *category_to_name __P((int category));
  156. static const char *guess_category_value __P((int category,
  157.                          const char *categoryname));
  158.  
  159.  
  160. /* Names for the libintl functions are a problem.  They must not clash
  161.    with existing names and they should follow ANSI C.  But this source
  162.    code is also used in GNU C Library where the names have a __
  163.    prefix.  So we have to make a difference here.  */
  164. #ifdef _LIBC
  165. # define DCGETTEXT __dcgettext
  166. #else
  167. # define DCGETTEXT dcgettext__
  168. #endif
  169.  
  170. /* Look up MSGID in the DOMAINNAME message catalog for the current CATEGORY
  171.    locale.  */
  172. char *
  173. DCGETTEXT (domainname, msgid, category)
  174.      const char *domainname;
  175.      const char *msgid;
  176.      int category;
  177. {
  178.   struct loaded_domain *domain;
  179.   struct binding *binding;
  180.   const char *categoryname;
  181.   const char *categoryvalue;
  182.   char *dirname, *xdomainname;
  183.   char *single_locale;
  184.   char *retval;
  185.  
  186.   /* If no real MSGID is given return NULL.  */
  187.   if (msgid == NULL)
  188.     return NULL;
  189.  
  190.   /* If DOMAINNAME is NULL, we are interested in the default domain.  If
  191.      CATEGORY is not LC_MESSAGES this might not make much sense but the
  192.      defintion left this undefined.  */
  193.   if (domainname == NULL)
  194.     domainname = _nl_current_default_domain;
  195.  
  196.   /* First find matching binding.  */
  197.   for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
  198.     {
  199.       int compare = strcmp (domainname, binding->domainname);
  200.       if (compare == 0)
  201.     /* We found it!  */
  202.     break;
  203.       if (compare < 0)
  204.     {
  205.       /* It is not in the list.  */
  206.       binding = NULL;
  207.       break;
  208.     }
  209.     }
  210.  
  211.   if (binding == NULL)
  212.     dirname = (char *) _nl_default_dirname;
  213.   else if (binding->dirname[0] == '/')
  214.     dirname = binding->dirname;
  215.   else
  216.     {
  217.       /* We have a relative path.  Make it absolute now.  */
  218.       size_t dirname_len = strlen (binding->dirname) + 1;
  219.       size_t path_max;
  220.       char *ret;
  221.  
  222.       path_max = (unsigned) PATH_MAX;
  223.       path_max += 2;        /* The getcwd docs say to do this.  */
  224.  
  225.       dirname = (char *) alloca (path_max + dirname_len);
  226.  
  227.       errno = 0;
  228.       while ((ret = getcwd (dirname, path_max)) == NULL && errno == ERANGE)
  229.     {
  230.       path_max += PATH_INCR;
  231.       dirname = (char *) alloca (path_max + dirname_len);
  232.       errno = 0;
  233.     }
  234.  
  235.       if (ret == NULL)
  236.     /* We cannot get the current working directory.  Don't signal an
  237.        error but simply return the default string.  */
  238.     return (char *) msgid;
  239.  
  240.       /* We don't want libintl.a to depend on any other library.  So
  241.      we avoid the non-standard function stpcpy.  In GNU C Library
  242.      this function is available, though.  Also allow the symbol
  243.      HAVE_STPCPY to be defined.  */
  244. #if defined _LIBC || defined HAVE_STPCPY
  245.       stpcpy (stpcpy (strchr (dirname, '\0'), "/"), binding->dirname);
  246. #else
  247.       strcat (dirname, "/");
  248.       strcat (dirname, binding->dirname);
  249. #endif
  250.     }
  251.  
  252.   /* Now determine the symbolic name of CATEGORY and its value.  */
  253.   categoryname = category_to_name (category);
  254.   categoryvalue = guess_category_value (category, categoryname);
  255.  
  256.   xdomainname = (char *) alloca (strlen (categoryname)
  257.                  + strlen (domainname) + 5);
  258.   /* We don't want libintl.a to depend on any other library.  So we
  259.      avoid the non-standard function stpcpy.  In GNU C Library this
  260.      function is available, though.  Also allow the symbol HAVE_STPCPY
  261.      to be defined.  */
  262. #if defined _LIBC || defined HAVE_STPCPY
  263.   stpcpy (stpcpy (stpcpy (stpcpy (xdomainname, categoryname), "/"),
  264.           domainname),
  265.       ".mo");
  266. #else
  267.   strcpy (xdomainname, categoryname);
  268.   strcat (xdomainname, "/");
  269.   strcat (xdomainname, domainname);
  270.   strcat (xdomainname, ".mo");
  271. #endif
  272.  
  273.   /* Creating working area.  */
  274.   single_locale = (char *) alloca (strlen (categoryvalue) + 1);
  275.  
  276.  
  277.   /* Search for the given string.  This is a loop because we perhaps
  278.      got an ordered list of languages to consider for th translation.  */
  279.   while (1)
  280.     {
  281.       /* Make CATEGORYVALUE point to the next element of the list.  */
  282.       while (categoryvalue[0] != '\0' && categoryvalue[0] == ':')
  283.     ++categoryvalue;
  284.       if (categoryvalue[0] == '\0')
  285.     {
  286.       /* The whole contents of CATEGORYVALUE has been searched but
  287.          no valid entry has been found.  We solve this situation
  288.          by implicitely appending a "C" entry, i.e. no translation
  289.          will take place.  */
  290.       single_locale[0] = 'C';
  291.       single_locale[1] = '\0';
  292.     }
  293.       else
  294.     {
  295.       char *cp = single_locale;
  296.       while (categoryvalue[0] != '\0' && categoryvalue[0] != ':')
  297.         *cp++ = *categoryvalue++;
  298.       *cp = '\0';
  299.     }
  300.  
  301.       /* If the current locale value is C (or POSIX) we don't load a
  302.      domain.  Return the MSGID.  */
  303.       if (strcmp (single_locale, "C") == 0
  304.       || strcmp (single_locale, "POSIX") == 0)
  305.     return (char *) msgid;
  306.  
  307.  
  308.       /* Find structure describing the message catalog matching the
  309.      DOMAINNAME and CATEGORY.  */
  310.       domain = _nl_find_domain (dirname, single_locale, xdomainname);
  311.  
  312.       if (domain != NULL)
  313.     {
  314.       retval = find_msg (domain, msgid);
  315.  
  316.       if (retval == NULL)
  317.         {
  318.           int cnt;
  319.  
  320.           for (cnt = 0; domain->successor[cnt] != NULL; ++cnt)
  321.         {
  322.           retval = find_msg (domain->successor[cnt], msgid);
  323.  
  324.           if (retval != NULL)
  325.             break;
  326.         }
  327.         }
  328.  
  329.       if (retval != NULL)
  330.         return retval;
  331.     }
  332.     }
  333.   /* NOTREACHED */
  334. }
  335.  
  336. #ifdef _LIBC
  337. /* Alias for function name in GNU C Library.  */
  338. weak_alias (__dcgettext, dcgettext);
  339. #elif defined(__sgi)
  340. #pragma weak dcgettext = dcgettext__
  341. #endif
  342.  
  343.  
  344. static char *
  345. find_msg (domain, msgid)
  346.      struct loaded_domain *domain;
  347.      const char *msgid;
  348. {
  349.   size_t top, act, bottom;
  350.  
  351.   if (domain->decided == 0)
  352.     _nl_load_domain (domain);
  353.  
  354.   if (domain->data == NULL)
  355.     return NULL;
  356.  
  357.   /* Locate the MSGID and its translation.  */
  358.   if (domain->hash_size > 2 && domain->hash_tab != NULL)
  359.     {
  360.       /* Use the hashing table.  */
  361.       nls_uint32 len = strlen (msgid);
  362.       nls_uint32 hash_val = hash_string (msgid);
  363.       nls_uint32 idx = hash_val % domain->hash_size;
  364.       nls_uint32 incr = 1 + (hash_val % (domain->hash_size - 2));
  365.       nls_uint32 nstr = W (domain->must_swap, domain->hash_tab[idx]);
  366.  
  367.       if (nstr == 0)
  368.     /* Hash table entry is empty.  */
  369.     return NULL;
  370.  
  371.       if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
  372.       && strcmp (msgid,
  373.              domain->data + W (domain->must_swap,
  374.                        domain->orig_tab[nstr - 1].offset)) == 0)
  375.     return (char *) domain->data + W (domain->must_swap,
  376.                       domain->trans_tab[nstr - 1].offset);
  377.  
  378.       while (1)
  379.     {
  380.       if (idx >= W (domain->must_swap, domain->hash_size) - incr)
  381.         idx -= W (domain->must_swap, domain->hash_size) - incr;
  382.       else
  383.         idx += incr;
  384.  
  385.       nstr = W (domain->must_swap, domain->hash_tab[idx]);
  386.       if (nstr == 0)
  387.         /* Hash table entry is empty.  */
  388.         return NULL;
  389.  
  390.       if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
  391.           && strcmp (msgid,
  392.              domain->data + W (domain->must_swap,
  393.                        domain->orig_tab[nstr - 1].offset))
  394.              == 0)
  395.         return (char *) domain->data
  396.           + W (domain->must_swap, domain->trans_tab[nstr - 1].offset);
  397.     }
  398.       /* NOTREACHED */
  399.     }
  400.  
  401.   /* Now we try the default method:  binary search in the sorted
  402.      array of messages.  */
  403.   bottom = 0;
  404.   top = domain->nstrings;
  405.   while (bottom < top)
  406.     {
  407.       int cmp_val;
  408.  
  409.       act = (bottom + top) / 2;
  410.       cmp_val = strcmp (msgid, domain->data
  411.                    + W (domain->must_swap,
  412.                     domain->orig_tab[act].offset));
  413.       if (cmp_val < 0)
  414.     top = act;
  415.       else if (cmp_val > 0)
  416.     bottom = act + 1;
  417.       else
  418.     break;
  419.     }
  420.  
  421.   /* If an translation is found return this.  */
  422.   return bottom >= top ? NULL : (char *) domain->data
  423.                                 + W (domain->must_swap,
  424.                      domain->trans_tab[act].offset);
  425. }
  426.  
  427.  
  428. /* Return string representation of locale CATEGORY.  */
  429. static const char *category_to_name (category)
  430.      int category;
  431. {
  432.   const char *retval;
  433.  
  434.   switch (category)
  435.   {
  436. #ifdef LC_COLLATE
  437.   case LC_COLLATE:
  438.     retval = "LC_COLLATE";
  439.     break;
  440. #endif
  441. #ifdef LC_CTYPE
  442.   case LC_CTYPE:
  443.     retval = "LC_CTYPE";
  444.     break;
  445. #endif
  446. #ifdef LC_MONETARY
  447.   case LC_MONETARY:
  448.     retval = "LC_MONETARY";
  449.     break;
  450. #endif
  451. #ifdef LC_NUMERIC
  452.   case LC_NUMERIC:
  453.     retval = "LC_NUMERIC";
  454.     break;
  455. #endif
  456. #ifdef LC_TIME
  457.   case LC_TIME:
  458.     retval = "LC_TIME";
  459.     break;
  460. #endif
  461. #ifdef LC_MESSAGES
  462.   case LC_MESSAGES:
  463.     retval = "LC_MESSAGES";
  464.     break;
  465. #endif
  466. #ifdef LC_RESPONSE
  467.   case LC_RESPONSE:
  468.     retval = "LC_RESPONSE";
  469.     break;
  470. #endif
  471. #ifdef LC_ALL
  472.   case LC_ALL:
  473.     /* This might not make sense but is perhaps better than any other
  474.        value.  */
  475.     retval = "LC_ALL";
  476.     break;
  477. #endif
  478.   default:
  479.     /* If you have a better idea for a default value let me know.  */
  480.     retval = "LC_XXX";
  481.   }
  482.  
  483.   return retval;
  484. }
  485.  
  486. /* Guess value of current locale from value of the environment variables.  */
  487. static const char *guess_category_value (category, categoryname)
  488.      int category;
  489.      const char *categoryname;
  490. {
  491.   const char *retval;
  492.  
  493.   /* The highest priority value is the `LANGUAGE' environment
  494.      variable.  This is a GNU extension.  */
  495.   retval = getenv ("LANGUAGE");
  496.   if (retval != NULL && retval[0] != '\0')
  497.     return retval;
  498.  
  499.   /* `LANGUAGE' is not set.  So we have to proceed with the POSIX
  500.      methods of looking to `LC_ALL', `LC_xxx', and `LANG'.  On some
  501.      systems this can be done by the `setlocale' function itself.  */
  502. #if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
  503.   return setlocale (category, NULL);
  504. #else
  505.   /* Setting of LC_ALL overwrites all other.  */
  506.   retval = getenv ("LC_ALL");
  507.   if (retval != NULL && retval[0] != '\0')
  508.     return retval;
  509.  
  510.   /* Next comes the name of the desired category.  */
  511.   retval = getenv (categoryname);
  512.   if (retval != NULL && retval[0] != '\0')
  513.     return retval;
  514.  
  515.   /* Last possibility is the LANG environment variable.  */
  516.   retval = getenv ("LANG");
  517.   if (retval != NULL && retval[0] != '\0')
  518.     return retval;
  519.  
  520.   /* We use C as the default domain.  POSIX says this is implementation
  521.      defined.  */
  522.   return "C";
  523. #endif
  524. }
  525.